package com.daxt.common.config.cas;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.daxt.base.common.util.RedisUtil;
import com.daxt.mapper.sys.SystemConfigMapper;
import com.daxt.model.dic.LoginPattern;
import com.daxt.model.dic.SystemConfigType;
import com.daxt.model.service.base.vo.SystemConfig;
import net.unicon.cas.client.configuration.CasClientConfigurerAdapter;
import net.unicon.cas.client.configuration.EnableCasClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.HashMap;
import java.util.Map;

import static com.daxt.model.dic.LoginPattern.*;

@EnableCasClient    //  启动CAS
@Configuration
public class CasConfiguration extends CasClientConfigurerAdapter {
//    @Value("${ignore-pattern}")
//    private String ignorePattern;
    @Autowired
    private SystemConfigMapper systemConfigMapper;

    @Override
    public void configureAuthenticationFilter(FilterRegistrationBean authenticationFilter) {
        super.configureAuthenticationFilter(authenticationFilter);

        SystemConfig sc = systemConfigMapper.selectOne(new QueryWrapper<SystemConfig>().eq("sysKey","LOGIN_PATTERN"));
        String lp = sc.getSysValue();

        if(lp.equals(SSO.getKey())){
            //配置地址，这里还可以配置很多，例如cas重定向策略等。
            //authenticationFilter.getInitParameters().put("ignorePattern", "/web/borrow/approval/*");
            //webservice接口地址
            //authenticationFilter.getInitParameters().put("ignorePattern", "/receives/*");
            authenticationFilter.getInitParameters().put("ignorePattern", "(/web/borrow/approval/*)|" +
                    "(/receives/*)|(/SystemConfig/LOGIN_PATTERN)|(/web/borrow/applyDetails/*)|" +
                    "(/acceptor/work/)|(/SystemConfig/VERSIONS_MARK)|(/static/*)");

        }else{
            authenticationFilter.getInitParameters().put("ignorePattern", "/*");
        }

    }
}
